package com.gitee.feizns.explore.data_structure.tree.bs.mid;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 513. 找树左下角的值
 * @author feizns
 * @since 2020/3/10
 */
public class FindBottomLeftValue {

    public static void main(String[] args) {

    }

    public static int findBottomLeftValue(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        if ( root != null )
            queue.add(root);
        while ( !queue.isEmpty() ) {
            int size = queue.size();
            TreeNode left = queue.peek();
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.remove();
                if ( node.left != null )
                    queue.add(node.left);
                if ( node.right != null )
                    queue.add(node.right);
            }
            if ( queue.isEmpty() )
                return left.val;
        }
        return -1;
    }

}
